# -*- coding: utf-8 -*-

import datetime
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import hashlib
import json
import time

from pymongo import MongoClient
from redis import StrictRedis
from scrapy.exceptions import DropItem


class CheckExist:
    def open_spider(self, spider):
        # 链接Redis
        self.redis_client = StrictRedis(host='localhost', port=6379, db=5)

    def close_spider(self, spider):
        # 关闭Redis的链接
        self.redis_client.close()

    def process_item(self, item, spider):
        # 目的：检查Redis中是否存在此数据，如果存在则爆出异常从而实现传递给下一个管道从而不存储
        # 将item中的数据进行hash，然后拿着这个hash值到Redis中查询
        print("----Redis检查管道---")
        waitting_hash_str = json.dumps(item)
        md5 = hashlib.md5()
        md5.update(waitting_hash_str.encode())
        hash_val = md5.hexdigest()
        # 1. 判断Redis中是否存在
        exist_flag = self.redis_client.get(hash_val)
        if exist_flag:
            # 2. 如果有则抛出异常
            raise DropItem("----此数据丢弃，因为Redis中查询到了----")
        else:
            # 3. 如果没有则存储到Redis且返回item
            self.redis_client.set(hash_val, waitting_hash_str)
        return item


class Myscrapy4Pipeline(object):
    def process_item(self, item, spider):
        # print(item)
        year = datetime.datetime.now().year
        month = datetime.datetime.now().month
        day = datetime.datetime.now().day
        price = item.get('price')
        # print(price)
        a1 = price.replace('元', " ")
        a2 = a1.replace('（', ' ')
        price = a2.replace('）', ' ')

        item['price'] = price
        print(item)

        date = item.get("date")
        print(date)
        if ":" in date:
            date = '%s年%s月%s日' % (year, month, day)
            item["date"] = date
        else:
            date_old = '%s年%s月%s日' % (year, month, day)
            print(date_old)
            date_old = date_old.replace(date_old[5:7], date[0:2])
            temp = date[3:]
            if temp[:-1] == '0':
                temp = temp.replace("0", "")
            data_new = date_old.replace(date_old[8:10], temp)
            item["date"] = data_new


class Save(object):
    def open_spider(self, spider):
        # 连接mongo数据库
        self.client = MongoClient("127.0.0.1", 27017)
        self.collection = self.client["info"]["info"]

    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):
        self.collection.insert_one(item)
        return item
